package com.tle.eventexample;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.tle.libs.CONSTANTS.FONT_PATHS;
import com.tle.libs.CONSTANTS.MARGINS;
import com.tle.libs.CONSTANTS.TEXT_SIZES;
import com.tle.libs.UIUtilities;

public class BMI extends BaseActivity 
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Properties //
	////////////////////////////////////////////////////////////////////////////////////////////////////	
	protected TableLayout m_pnlMain;
	
	protected TextView m_lblName;
	protected EditText m_txtName;
	
	protected TextView m_lblHeight;
	protected EditText m_txtHeight;
	
	protected TextView m_lblWeight;
	protected EditText m_txtWeight;
	
	protected Button m_btnCalculateBMI;
	protected Button m_btnComeBack;
	
	protected TextView m_lblBMI;
	protected EditText m_txtBMI;
	
	protected TextView m_lblResult;
	protected EditText m_txtResult;
	
	protected int m_nCol1Width = 80;
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																						// Properties //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Constructor and Support Method //
	////////////////////////////////////////////////////////////////////////////////////////////////////
		
	public void setActivityTitle() 
	{
		this.setTitle(R.string.title_BMICalculator);
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		this.initComponents();
		
//		this.initGUIOld();
		this.initGUINew();
		
		UIUtilities.setUIStandard(m_pnlMain , FONT_PATHS.PLAINT , TEXT_SIZES.NORMAL);
		this.registerEvent();
	}
	
//	protected void initGUIOld ()
//	{
//		LayoutParams lotParamMain = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//		lotParamMain.setMargins(MARGINS.M5.getValue(), 0, MARGINS.M5.getValue(), 0);
//		
//		
//		
//		LayoutParams lotParamCol1 = new LayoutParams(m_nCol1Width, LayoutParams.WRAP_CONTENT);
//		lotParamCol1.rightMargin = CONSTANTS.MARGINS.M5.getValue();
//		
//		LayoutParams lotParamCol2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//		
//		
//		
//		LayoutParams lotParamButton = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//		lotParamButton.setMargins(MARGINS.M2.getValue(), 0, MARGINS.M2.getValue(), 0);
//		
//		
//		
//		this.setTitle(R.string.title_TinhChiSoBMI);
//		
//		
//		
//		m_lotMain = new LinearLayout(this);
//		this.setContentView(m_lotMain);
//		
//		m_lotMain.setOrientation(LinearLayout.VERTICAL);
//		
//		
//		
//		
//		
//		m_lblName = new TextView(this);
//		m_lblName.setGravity(Gravity.RIGHT);
//		m_lblName.setText("Tên");
//		
//		m_txtName = new EditText(this);
//		m_txtName.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//		
//		m_lotMain.addView( UIUtilities.buildLayoutWith2Cols(m_lblName, lotParamCol1, m_txtName, lotParamCol2) , lotParamMain); 
//		
//		
//		
//		m_lblHeight = new TextView(this);
//		m_lblHeight.setGravity(Gravity.RIGHT);
//		m_lblHeight.setText("Chiều cao");	
//		
//		m_txtHeight = new EditText(this);
//		m_txtHeight.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//		
//		m_lotMain.addView( UIUtilities.buildLayoutWith2Cols(m_lblHeight, lotParamCol1, m_txtHeight, lotParamCol2) , lotParamMain );
//		
//		
//		
//		m_lblWeight = new TextView(this);
//		m_lblWeight.setGravity(Gravity.RIGHT);
//		m_lblWeight.setText("Cân nặng");	
//		
//		m_txtWeight = new EditText(this);
//		m_txtWeight.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//		
//		m_lotMain.addView( UIUtilities.buildLayoutWith2Cols(m_lblWeight, lotParamCol1, m_txtWeight, lotParamCol2) , lotParamMain );
//		
//		
//		
//		m_btnCalculateBMI = new Button(this);
//		m_btnCalculateBMI.setText("Tính BMI");
//		m_lotMain.addView(m_btnCalculateBMI , lotParamButton);
//		
//		
//		
//		m_lblBMI = new TextView(this);
//		m_lblBMI.setGravity(Gravity.RIGHT);
//		m_lblBMI.setText("BMI");	
//		
//		m_txtBMI = new EditText(this);
//		m_txtBMI.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//		
//		m_lotMain.addView( UIUtilities.buildLayoutWith2Cols(m_lblBMI, lotParamCol1, m_txtBMI, lotParamCol2) , lotParamMain );
//		
//		
//		
//		m_lblResult = new TextView(this);
//		m_lblResult.setGravity(Gravity.RIGHT);
//		m_lblResult.setText("Chuẩn đoán");	
//		
//		m_txtResult = new EditText(this);
//		m_txtResult.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//		
//		m_lotMain.addView( UIUtilities.buildLayoutWith2Cols(m_lblResult, lotParamCol1, m_txtResult, lotParamCol2) , lotParamMain );
//		
//		
//		
//		m_btnComeBack = new Button(this);
//		m_btnComeBack.setText("Trở về menu chính");
//		m_lotMain.addView(m_btnComeBack , lotParamButton);
//	}
	
	protected void initComponents()
	{
		m_lblName = new TextView(this);
		m_lblName.setText(R.string.BMI_Name);
		
		m_txtName = new EditText(this);
		
		
		
		m_lblBMI = new TextView(this);
		m_lblBMI.setText(R.string.BMI_BMI);
		
		m_txtBMI = new EditText(this);
		m_txtBMI.setEnabled(false);
		
		
		
		m_lblHeight = new TextView(this);
		m_lblHeight.setText(R.string.BMI_Height);
		
		m_txtHeight = new EditText(this);
		m_txtHeight.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
		
		
		
		m_lblResult = new TextView(this);
		m_lblResult.setText(R.string.BMI_Result);
		
		m_txtResult = new EditText(this);
		m_txtResult.setEnabled(false);
		
		
		
		m_lblWeight = new TextView(this);
		m_lblWeight.setText(R.string.BMI_Weight);
		
		m_txtWeight = new EditText(this);
		m_txtWeight.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
		
		
		
		m_btnCalculateBMI = new Button(this);
		m_btnCalculateBMI.setText(R.string.BMI_CalculateBMI);
		
		
		
		m_btnComeBack = new Button(this);
		m_btnComeBack.setText(R.string.const_btnComeBack);
	}
	
	protected void initGUINew ()
	{		
		m_pnlMain = new TableLayout(this);
		//Khong co tac dung
//		LayoutParams mainParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//		mainParams.setMargins(MARGINS.M5.getValue(), 0, MARGINS.M5.getValue(), 0);
//		this.setContentView(m_pnlMain, mainParams);
		this.setContentView(m_pnlMain);
		
		
		
		TableRow.LayoutParams col1Params = new TableRow.LayoutParams();
		col1Params.setMargins(MARGINS.M5.getValue(), 0, MARGINS.M5.getValue(), 0);
		
		TableRow.LayoutParams col2Params = new TableRow.LayoutParams();
		// match parent
		col2Params.weight = 1.0f;
		col2Params.setMargins(0, 0, MARGINS.M5.getValue(), 0);
		
		TableRow.LayoutParams spanParams = new TableRow.LayoutParams();
		spanParams.weight = 1.0f;
		spanParams.setMargins(MARGINS.M2.getValue(), 0, MARGINS.M2.getValue(), 0);
		spanParams.span = 2;
		
		
		
		TableRow row = new TableRow(this);
		row.addView(m_lblName, col1Params);
		row.addView(m_txtName, col2Params);
		m_pnlMain.addView(row);
		
		
		
		row = new TableRow(this); 
		row.addView(m_lblHeight, col1Params);
		row.addView(m_txtHeight, col2Params);
		m_pnlMain.addView(row);
		
		
		
		row = new TableRow(this); 
		row.addView(m_lblWeight, col1Params);
		row.addView(m_txtWeight, col2Params);
		m_pnlMain.addView(row);
		
		
		
		row = new TableRow(this);
		row.addView(m_btnCalculateBMI, spanParams);
		m_pnlMain.addView(row);
		
		
		
		row = new TableRow(this); 
		row.addView(m_lblBMI, col1Params);
		row.addView(m_txtBMI, col2Params);
		m_pnlMain.addView(row);
		
		
		
		row = new TableRow(this); 
		row.addView(m_lblResult, col1Params);
		row.addView(m_txtResult, col2Params);
		m_pnlMain.addView(row);
		
		
		
		row = new TableRow(this);
		row.addView(m_btnComeBack, spanParams);
		m_pnlMain.addView(row);
	}
	
	
	
	protected void registerEvent ()
	{		
		ButtonEvent buttonEvent = new ButtonEvent();
		m_btnComeBack.setOnClickListener(buttonEvent);
		m_btnCalculateBMI.setOnClickListener(buttonEvent);
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																	// Constructor and Support Method //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Support class and methods //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	private class ButtonEvent implements OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			if (v == m_btnComeBack)
			{
				finish();

			}// end m_btnComeBack
			
			else if (v == m_btnCalculateBMI)
			{
				calculateBMI();
				UIUtilities.hideSoftKeyboard(BMI.this);
			}// end m_btnCalculateBMI
			
		}
	}
	
	private void calculateBMI ()
	{
		if (isValidInput() == false)
			return;
		
		double dbHeight = Double.parseDouble(m_txtHeight.getText().toString());		
		double dbWeight = Double.parseDouble(m_txtWeight.getText().toString());
		
		BigDecimal bdBMI = new BigDecimal(dbWeight / Math.pow(dbHeight, 2));
				
		DecimalFormat decimalFormat = new DecimalFormat();
		decimalFormat.setMinimumFractionDigits(0);
		decimalFormat.setMaximumFractionDigits(2);
		m_txtBMI.setText( decimalFormat.format(bdBMI.doubleValue()) );
		
		float ftBMI = bdBMI.floatValue();
		
		if (ftBMI < 18)
			m_txtResult.setText(R.string.BMI_ResSkinny); // Gay
		
		else if (18 <= ftBMI && ftBMI <= 24.9)
			m_txtResult.setText(R.string.BMI_ResGood); // Binh thuong
		
		else if (25 <= ftBMI && ftBMI <= 29.9)
			m_txtResult.setText(R.string.BMI_ResObesityLevelI); // Beo phi loai I
		
		else if (30 <= ftBMI && ftBMI <= 34.9)
			m_txtResult.setText(R.string.BMI_ResObesityLevelII); // Beo phi loai II
		
		else if (ftBMI > 35)
			m_txtResult.setText(R.string.BMI_ResObesityLevelIII); // Beo phi loai III
	}
	
	private boolean isValidInput ()
	{
		boolean bBlankHeight = m_txtHeight.getText().toString().isEmpty();
		boolean bBlankWeight = m_txtWeight.getText().toString().isEmpty();
		
		boolean isDotHeight = m_txtHeight.getText().toString().equals(".");
		boolean isDotWeight = m_txtWeight.getText().toString().equals(".");
		
		if (bBlankWeight == true || bBlankHeight == true || isDotHeight == true || isDotWeight == true)
			return false;
		
		return true;
	}
		
	////////////////////////////////////////////////////////////////////////////////////////////////////
																		 // Support class and methods //
	////////////////////////////////////////////////////////////////////////////////////////////////////
}
